//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.promotion;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.PromotionCardTypeDao;
import com.aidong.dao.wills.PromotionDao;
import com.aidong.dao.wills.PssProductCodeDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.Promotion;
import com.aidong.model.wills.PromotionCardType;
import com.aidong.model.wills.PssProductCode;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"promotion"})
public class PromotionController extends BaseController {
    @Resource
    PromotionDao promotionDao;
    @Resource
    PromotionCardTypeDao promotionCardTypeDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    PssProductCodeDao pssProductCodeDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;

    public PromotionController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("promotion/promotion/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.like("chName", "%" + name + "%");
        this.promotionDao.queryPageList(pg, cd, (new OrderSet()).asc("promotionId"));
        ModelAndView mv = new ModelAndView("promotion/promotion/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        Promotion promotion = new Promotion();
        promotion.setStatus(Integer.valueOf(1));
        List shopList = this.dicShopCodeDao.getAllShopList();
        Iterator productList = shopList.iterator();

        while(productList.hasNext()) {
            DicShopCode cardList = (DicShopCode)productList.next();
            cardList.setStatus(Integer.valueOf(0));
        }

        List productList1 = this.pssProductCodeDao.getValidProductList("2");
        Iterator cardList1 = productList1.iterator();

        while(cardList1.hasNext()) {
            PssProductCode cardTypeList = (PssProductCode)cardList1.next();
            cardTypeList.setStatus(Integer.valueOf(0));
        }

        ArrayList cardList2 = new ArrayList();
        List cardTypeList1 = this.cardTypeCodeDao.getPromotionCardList();
        ModelAndView mv = new ModelAndView("promotion/promotion/edit");
        mv.addObject("promotion", promotion);
        mv.addObject("shopList", shopList);
        mv.addObject("productList", productList1);
        mv.addObject("cardList", cardList2);
        mv.addObject("cardTypeList", cardTypeList1);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("promotion") Promotion promotion, @RequestParam(value = "shops",required = false) String shops, @RequestParam(value = "prods",required = false) String prods, @RequestParam(value = "cards",required = false) String cards) {
        if(this.chkNull(promotion.getChName())) {
            return this.getResult(false, "请输入名称");
        } else {
            if(this.chkNullZ(promotion.getStatus())) {
                promotion.setStatus(Integer.valueOf(1));
            }

            promotion.setCreateUser(this.getLoginUserCode());
            promotion.setCreateTime(new Date());
            promotion.setUpdateTime(new Date());
            ArrayList shopList = new ArrayList();
            String[] cardList;
            int len$;
            if(!this.chkNull(shops)) {
                String[] productList = shops.split(",");
                cardList = productList;
                int e = productList.length;

                for(len$ = 0; len$ < e; ++len$) {
                    String i$ = cardList[len$];
                    if(!this.chkNull(i$)) {
                        shopList.add(i$.trim());
                    }
                }
            }

            ArrayList var13 = new ArrayList();
            if(!this.chkNull(prods)) {
                cardList = prods.split(",");
                String[] var15 = cardList;
                len$ = cardList.length;

                for(int var16 = 0; var16 < len$; ++var16) {
                    String s = var15[var16];
                    if(!this.chkNull(s)) {
                        var13.add(s.trim());
                    }
                }
            }

            List var14 = this.getCardType(cards);
            if(var14 == null) {
                return this.getResult(false, "数据错误");
            } else {
                try {
                    this.promotionDao.addPromotion(promotion, shopList, var13, var14);
                    this.addUserLog("新增营销活动", "编号:" + promotion.getPromotionId() + ",名称:" + promotion.getChName());
                } catch (Exception var12) {
                    this.log.error("add promotion", var12);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    private List<PromotionCardType> getCardType(String cards) {
        ArrayList list = new ArrayList();
        if(!this.chkNull(cards)) {
            String[] ss = cards.split("\\|");
            String[] arr$ = ss;
            int len$ = ss.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String s1 = arr$[i$];
                String[] s = s1.split(",");
                Integer id = this.parseInt(s[0]);
                PromotionCardType cardType;
                if(s.length == 2 && "-".equals(s[1]) && !this.chkNullZ(id)) {
                    cardType = new PromotionCardType(id);
                    cardType.setPromotionId(Integer.valueOf(-1));
                } else {
                    if(s.length <= 2) {
                        return null;
                    }

                    cardType = new PromotionCardType(id);
                    cardType.setCardType(s[1]);
                    cardType.setPrice(this.parseDouble(s[2]));
                }

                list.add(cardType);
            }
        }

        return list;
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("id") int id) {
        Promotion promotion = (Promotion)this.promotionDao.getById(Integer.valueOf(id));
        if(promotion == null) {
            return this.showAlert("没有这个记录");
        } else {
            List shopList = this.promotionDao.getShopList(id, true);
            List productList = this.promotionDao.getProductList(id, true);
            List cardList = this.promotionCardTypeDao.getCardTypeList(id);
            List cardTypeList = this.cardTypeCodeDao.getPromotionCardList();
            ModelAndView mv = new ModelAndView("promotion/promotion/edit");
            mv.addObject("promotion", promotion);
            mv.addObject("shopList", shopList);
            mv.addObject("productList", productList);
            mv.addObject("cardList", cardList);
            mv.addObject("cardTypeList", cardTypeList);
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("promotion") Promotion promotion, @RequestParam(value = "shops",required = false) String shops, @RequestParam(value = "prods",required = false) String prods, @RequestParam(value = "cards",required = false) String cards) {
        if(this.chkNull(promotion.getPromotionId())) {
            return this.getResult(false, "没有这个记录");
        } else if(this.chkNull(promotion.getChName())) {
            return this.getResult(false, "请输入名称");
        } else {
            if(this.chkNullZ(promotion.getStatus())) {
                promotion.setStatus(Integer.valueOf(1));
            }

            Promotion promotion1 = (Promotion)this.promotionDao.getById(promotion.getPromotionId());
            if(promotion1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                promotion1.setChName(promotion.getChName());
                promotion1.setActiveTime(promotion.getActiveTime());
                promotion1.setInactiveTime(promotion.getInactiveTime());
                promotion1.setNote(promotion.getNote());
                promotion1.setStatus(promotion.getStatus());
                promotion1.setUpdateTime(new Date());
                ArrayList shopList = new ArrayList();
                String[] cardList;
                int len$;
                if(!this.chkNull(shops)) {
                    String[] productList = shops.split(",");
                    cardList = productList;
                    int e = productList.length;

                    for(len$ = 0; len$ < e; ++len$) {
                        String i$ = cardList[len$];
                        if(!this.chkNull(i$)) {
                            shopList.add(i$.trim());
                        }
                    }
                }

                ArrayList var14 = new ArrayList();
                if(!this.chkNull(prods)) {
                    cardList = prods.split(",");
                    String[] var16 = cardList;
                    len$ = cardList.length;

                    for(int var17 = 0; var17 < len$; ++var17) {
                        String s = var16[var17];
                        if(!this.chkNull(s)) {
                            var14.add(s.trim());
                        }
                    }
                }

                List var15 = this.getCardType(cards);
                if(var15 == null) {
                    return this.getResult(false, "数据错误");
                } else {
                    try {
                        this.promotionDao.updatePromotion(promotion1, shopList, var14, var15);
                        this.addUserLog("修改营销活动", "编号:" + promotion.getPromotionId() + ",名称:" + promotion.getChName());
                    } catch (Exception var13) {
                        this.log.error("edit promotion", var13);
                        return this.getResult(false, "保存发生错误");
                    }

                    return this.getResult(true, "保存完成");
                }
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("id") int id) {
        Promotion promotion = (Promotion)this.promotionDao.getById(Integer.valueOf(id));
        if(promotion == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            try {
                this.promotionDao.updateStatus(id, 0);
                this.addUserLog("删除营销活动", "编号:" + promotion.getPromotionId() + ",名称:" + promotion.getChName());
            } catch (Exception var4) {
                this.log.error("del promotion", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
